// 模拟用户数据
const mockUsers = [
  // 学生账户 - 学号为9位数字
  {
    id: 1,
    account: '202103001',
    password: '123456',
    name: '张三',
    email: 'zhangsan@example.com',
    userType: 'student',
    major: '计算机科学与技术',
    className: '计科2021-1班',
    department: null,
    status: 'active',
    avatar: null,
    createTime: '2024-03-20T10:30:00'
  },
  {
    id: 2,
    account: '202103002',
    password: '123456',
    name: '李四',
    email: 'lisi@example.com',
    userType: 'student',
    major: '软件工程',
    className: '软工2021-2班',
    department: null,
    status: 'active',
    avatar: null,
    createTime: '2024-03-20T10:30:00'
  },
  {
    id: 3,
    account: '202103003',
    password: '123456',
    name: '王五',
    email: 'wangwu@example.com',
    userType: 'student',
    major: '网络工程',
    className: '网工2021-1班',
    department: null,
    status: 'active',
    avatar: null,
    createTime: '2024-03-20T10:30:00'
  },
  // 教师账户 - 工号为9位数字
  {
    id: 4,
    account: '202001001',
    password: '123456',
    name: '赵老师',
    email: 'zhao@example.com',
    userType: 'teacher',
    major: null,
    className: null,
    department: '计算机学院',
    status: 'active',
    avatar: null,
    createTime: '2024-03-20T10:30:00'
  },
  {
    id: 5,
    account: '202001002',
    password: '123456',
    name: '钱老师',
    email: 'qian@example.com',
    userType: 'teacher',
    major: null,
    className: null,
    department: '软件学院',
    status: 'active',
    avatar: null,
    createTime: '2024-03-20T10:30:00'
  },
  {
    id: 6,
    account: '202001003',
    password: '123456',
    name: '孙老师',
    email: 'sun@example.com',
    userType: 'teacher',
    major: null,
    className: null,
    department: '网络工程学院',
    status: 'active',
    avatar: null,
    createTime: '2024-03-20T10:30:00'
  }
]

// 模拟JWT token生成
function generateMockToken(user) {
  const header = btoa(JSON.stringify({ alg: 'HS256', typ: 'JWT' }))
  const payload = btoa(JSON.stringify({
    sub: user.id.toString(),
    account: user.account,
    userType: user.userType,
    iat: Math.floor(Date.now() / 1000),
    exp: Math.floor(Date.now() / 1000) + (24 * 60 * 60) // 24小时过期
  }))
  const signature = btoa('mock-signature')
  return `${header}.${payload}.${signature}`
}

// 模拟登录API
export function mockLogin(account, password) {
  return new Promise((resolve, reject) => {
    // 模拟网络延迟
    setTimeout(() => {
      const user = mockUsers.find(u => u.account === account && u.password === password)
      
      if (!user) {
        reject(new Error('账号或密码错误'))
        return
      }
      
      if (user.status !== 'active') {
        reject(new Error('账号已被禁用'))
        return
      }
      
      const token = generateMockToken(user)
      const userInfo = { ...user }
      delete userInfo.password // 不返回密码
      
      resolve({
        token,
        expiresIn: 24 * 60 * 60, // 24小时
        userInfo
      })
    }, 500) // 模拟500ms延迟
  })
}

// 模拟获取用户信息API
export function mockGetUserInfo(token) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      try {
        // 简单的token解析（实际项目中应该验证签名）
        const parts = token.split('.')
        if (parts.length !== 3) {
          reject(new Error('无效的token'))
          return
        }
        
        const payload = JSON.parse(atob(parts[1]))
        const user = mockUsers.find(u => u.id.toString() === payload.sub)
        
        if (!user) {
          reject(new Error('用户不存在'))
          return
        }
        
        const userInfo = { ...user }
        delete userInfo.password
        
        resolve(userInfo)
      } catch (error) {
        reject(new Error('token解析失败'))
      }
    }, 200)
  })
}

// 导出所有用户信息（用于调试）
export function getAllMockUsers() {
  return mockUsers.map(user => ({
    account: user.account,
    password: user.password,
    name: user.name,
    userType: user.userType,
    major: user.major,
    className: user.className,
    department: user.department
  }))
}
